package com.zhku.zeng.util;

import com.zhku.zeng.plugin.entity.RowEntity;
import com.zhku.zeng.plugin.entity.TableEntity;
import freemarker.template.TemplateException;
import lombok.Data;
import org.dom4j.Attribute;
import org.dom4j.DocumentException;
import org.dom4j.Element;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Data
public class ParseXmlUtil {

    private List<TableEntity> tableEntities = new ArrayList<TableEntity>();

    public ParseXmlUtil() {

    }

    public void parse(String filePath) throws DocumentException, ClassNotFoundException {
        Dom4jUtil dom4jUtil = new Dom4jUtil(filePath);
        Element root = dom4jUtil.getRoot();
        List<Element> tables = dom4jUtil.getChildren(root);
        if (tables.isEmpty()) {
            return;
        }
        for (Element table : tables) {
            TableEntity tableEntity = new TableEntity();
            tableEntity.setTableName(table.attribute("name").getValue());
            List<Element> rowTypes = dom4jUtil.getChildren(table);
            List<RowEntity> rowEntities = new ArrayList<RowEntity>();
//            遍历类型标签 如 Integer String
            for (Element rowType : rowTypes) {
                String type = rowType.getQualifiedName();
                List<Element> rowName = dom4jUtil.getChildren(rowType);
//                设置类型里面的值 如 value length
                for (Element element : rowName) {
                    RowEntity rowEntity = new RowEntity();
//                    设置type 类型
                    rowEntity.setType(Class.forName(type));
//                    设置length 有就设置没有就 null
                    Attribute length = element.attribute("length");
                    if (length != null) {
                        rowEntity.setLength(Integer.valueOf(length.getValue()));
                    }
//                    设置名字
                    rowEntity.setName(element.attribute("value").getValue());
                    rowEntities.add(rowEntity);
                }
            }
            tableEntity.setRows(rowEntities);
            tableEntities.add(tableEntity);
        }

    }

//    public static void main(String[] args) throws ClassNotFoundException, DocumentException, IOException, TemplateException {
//
//        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
//        File file = new File(contextClassLoader.getResource("./").getPath());
//        String absolutePath = file.getAbsolutePath();
//        System.out.println(absolutePath);
//        ParseXmlUtil parseXmlUtil = new ParseXmlUtil();
//        parseXmlUtil.parse(absolutePath+"/tables.xml");
//        List<TableEntity> tableEntities = parseXmlUtil.getTableEntities();
//        Map<String, Object> data = new HashMap<>();
//        data.put("table",tableEntities.get(0));
//        FreeMarkUtil freeMarkUtil = new FreeMarkUtil();
//        freeMarkUtil.parse("template.ftl","test.java",data);
//        System.out.println(tableEntities);
//
//    }


}
